<?php
class Tourism_goods_bargaining_model extends BZ_Model
{
    protected $table   = 'tourism_goods_bargaining';
    protected $table_1 = 'user';
    
    public function total($goods_attr_id){
        $this->db->select('*');
        $this->db->from($this->table.' AS tourism_goods_bargaining');
        $this->db->join($this->table_1.' AS user', 'tourism_goods_bargaining.seller_uid=user.uid','inner');
        $this->db->where('goods_attr_id', $goods_attr_id);
        return $this->db->count_all_results();
    }
    
    public function page_list($num,$page_num,$goods_attr_id){
        $this->db->select('tourism_goods_bargaining.*,user.alias_name,user.user_name');
        $this->db->from($this->table.' AS tourism_goods_bargaining');
        $this->db->join($this->table_1.' AS user','tourism_goods_bargaining.seller_uid=user.uid','inner');
        $this->db->where('tourism_goods_bargaining.goods_attr_id', $goods_attr_id);
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    public function findByArray($postData){
        $this->db->select('*');
        $this->db->from($this->table);
        
        if (!empty($postData['seller_uid'])){
            $this->db->where_in('seller_uid', $postData['seller_uid']);
        }
        
        if (!empty($postData['goods_attr_id'])){
            $this->db->where('goods_attr_id', $postData['goods_attr_id']);
        }
        
        $result = $this->db->get();
        if ($result->num_rows() > 0){
               return $result;
        }
        return false;
    }
    
    public function findById($id){
        $this->db->select('tourism_goods_bargaining.*,user.alias_name,user.user_name');
        $this->db->from($this->table.' AS tourism_goods_bargaining');
        $this->db->join($this->table_1.' AS user', 'tourism_goods_bargaining.seller_uid=user.uid','inner');
        $this->db->where('bargaining_id', $id);
        $this->db->limit(1);
        $result = $this->db->get();
        if ($result->num_rows() > 0){
            return $result;
        }
        return false;
    }    
    
    public function add($postData, $uid){
        $data = array(
            'goods_attr_id'    => $postData['goods_attr_id'],
            'seller_uid'       => $uid,
            'provider_uid'     => $postData['provider_uid'],
            'node'             => $postData['node'],
            'poundage'         => $postData['poundage'],
            'shop_price'       => $postData['shop_price'],
            'supply_price'     => $postData['supply_price'],
            'payer'            => $postData['payer'],
            'created_at'       => date('Y-m-d H:i:s'),
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function edit($postData){
        $data = array(
            'node'             => $postData['node'],
            'poundage'         => $postData['poundage'],
            'shop_price'       => $postData['shop_price'],
            'supply_price'     => $postData['supply_price'],
            'payer'            => $postData['payer'],
            'updated_at'       => date('Y-m-d H:i:s')
        );
        return $this->db->update($this->table, $data, array('bargaining_id'=>$postData['bargaining_id']));
    }
    
    public function delete($id){
        return $this->db->delete($this->table, array('bargaining_id'=>$id));
    }
    
}









